<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JavaScript 严格模式(use strict)</h1>

<p>JavaScript 严格模式（strict mode）即在严格的条件下运行。 </p>
<hr>
<h2>使用 "use strict" 指令</h2>
<p>"use strict" 指令在 JavaScript 1.8.5 (ECMAScript5) 中新增。</p>
<p>它不是一条语句，但是是一个字面量表达式，在 JavaScript 旧版本中会被忽略。</p>
<p>"use strict" 的目的是指定代码在严格条件下执行。</p>
<p>严格模式下你不能使用未声明的变量。</p>
<br>
<table class="lamp"><tbody><tr>
<th style="width:34px"><img decoding="async" src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px"></th>
<td>支持严格模式的浏览器:<br>Internet Explorer 10 +、Firefox 4+、 Chrome 13+、 Safari 5.1+、 Opera 12+。</td>
</tr></tbody></table>
<hr>
<h2>严格模式声明</h2>
<p>严格模式通过在脚本或函数的头部添加 <span class="marked">use strict</span>; 表达式来声明。</p>
<p>实例中我们可以在浏览器按下 <strong>F12 (或点击"工具&gt;更多工具&gt;开发者工具")</strong>  开启调试模式，查看报错信息。</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2016/05/74151F80-4F7D-4492-8288-1231025B41B6.jpeg" width="80%"></p>


<p>也可以通过右击鼠标，选择 <strong>"检查"</strong> 来查看，Gif 图演示如下：</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2016/05/js_strict.gif"></p>

<div class="example">
<h3>实例</h3>
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br>x = <span style="color:red">3.14</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错 (x 未定义)<br></span>
</span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;x = 3.14;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错 (x 未定义)"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_variable" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div><br>
<div class="example">
<h3>实例</h3>
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br>myFunction();<br><br>
<span style="color:mediumblue">function</span> myFunction() {<br><span style="color:red">
</span> &nbsp;&nbsp;&nbsp; y = <span style="color:red">3.14</span>;&nbsp;&nbsp; <span style="color:red">
</span> <span style="color:green">// 报错 (y 未定义)<br></span>}</span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;myFunction();
function myFunction() {
 &nbsp;&nbsp;&nbsp; y = 3.14;&nbsp;&nbsp; 
 // 报错 (y 未定义)}"></button></div>

<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_global" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<p>在函数内部声明是局部作用域 (只在函数内使用严格模式):</p>

<div class="example">
<h3>实例</h3>
<div class="example_code"><span style="color:black">
x = <span style="color:red">3.14</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 不报错
 <br></span>myFunction();<br><br><span style="color:mediumblue">function</span> <span style="color:red">
</span> myFunction() {<br><span style="color:red">
</span>&nbsp;&nbsp;&nbsp;<span style="color:brown">"use strict"</span>;<br><span style="color:red">
</span> &nbsp;&nbsp;&nbsp; y = <span style="color:red">3.14</span>;&nbsp;&nbsp; <span style="color:red">
</span> <span style="color:green">// 报错 (y 未定义)<br></span>}<br>
</span><button class="copy-code-button" type="button" data-clipboard-text="x = 3.14;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 不报错
 myFunction();function 
 myFunction() {
&nbsp;&nbsp;&nbsp;&quot;use strict&quot;;
 &nbsp;&nbsp;&nbsp; y = 3.14;&nbsp;&nbsp; 
 // 报错 (y 未定义)}"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_local" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>
</div>
<hr>
<p>为什么使用严格模式:</p>
<ul>
消除Javascript语法的一些不合理、不严谨之处，减少一些怪异行为;

<li>消除代码运行的一些不安全之处，保证代码运行的安全；

</li><li>提高编译器效率，增加运行速度；

</li><li>为未来新版本的Javascript做好铺垫。
</li></ul>
<p>"严格模式"体现了Javascript更合理、更安全、更严谨的发展方向，包括IE 10在内的主流浏览器，都已经支持它，许多大项目已经开始全面拥抱它。
</p><p>
另一方面，同样的代码，在"严格模式"中，可能会有不一样的运行结果；一些在"正常模式"下可以运行的语句，在"严格模式"下将不能运行。掌握这些内容，有助于更细致深入地理解Javascript，让你变成一个更好的程序员。

</p>
<hr>
<h2>严格模式的限制</h2>
<p>不允许使用未声明的变量：</p>

<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br>
x = <span style="color:red">3.14</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错 (x 未定义)
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;
x = 3.14;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错 (x 未定义)"></button></div>

<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_variable" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<table class="lamp"><tbody><tr>
<th style="width:34px">
<img decoding="async" src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px"></th>
<td>
对象也是一个变量。
</td>
</tr></tbody></table><br>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br>
x = <span style="color:red">
</span> {p1:<span style="color:red">10</span>, p2:<span style="color:red">20</span>};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错 (x 未定义)
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;
x = 
 {p1:10, p2:20};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错 (x 未定义)"></button></div>

<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_object" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<p>不允许删除变量或对象。</p>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">var</span> x = <span style="color:red">3.14</span>;<br>
<span style="color:mediumblue">delete</span> x;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;var x = 3.14;
delete x;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错"></button></div>

<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_delete" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<p>不允许删除函数。</p>

<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">function</span> x(p1, p2) {}; <br><span style="color:mediumblue">delete</span> x;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:red">
</span> <span style="color:green">// 报错&nbsp;
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;function x(p1, p2) {}; delete x;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_delete_function" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<p>不允许变量重名:</p>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">function</span> x(p1, p1) {};&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;function x(p1, p1) {};&nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_duplicate" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>



</div>
<p>不允许使用八进制:</p>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">var</span> x = <span style="color:red">010</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;var x = 010;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_octal" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>


</div><p>不允许使用转义字符:</p>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br>
<span style="color:mediumblue">var</span> x = \010;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;
var x = \010;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_escape" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<p>不允许对只读属性赋值:</p>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">var</span> obj = {};<br>
Object.defineProperty(obj, <span style="color:brown">"x"</span>, {value:<span style="color:red">0</span>, writable:<span style="color:mediumblue">false</span>});<br><br>
obj.x = <span style="color:red">3.14</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;var obj = {};
Object.defineProperty(obj, &quot;x&quot;, {value:0, writable:false});
obj.x = 3.14;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_readonly" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<p>不允许对一个使用getter方法读取的属性进行赋值</p>

<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">var</span> obj = {get x() <span style="color:red">
</span>{<span style="color:mediumblue">return</span> <span style="color:red">0</span>} };<br><br>obj.x = <span style="color:red">3.14</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;var obj = {get x() 
{return 0} };obj.x = 3.14;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_getonly" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>

<p>不允许删除一个不允许删除的属性：</p>

<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">delete</span> Object.prototype; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;delete Object.prototype; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_deleteprop" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div><p>变量名不能使用 "eval" 字符串:</p>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">var</span> <span style="color:mediumblue">eval</span> = <span style="color:red">3.14</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;var eval = 3.14;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_eval" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div><p>变量名不能使用 "arguments" 字符串:</p>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">var</span> <span style="color:mediumblue">arguments</span> = <span style="color:red">3.14</span>;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;var arguments = 3.14;&nbsp;&nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_arguments" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div><p>不允许使用以下这种语句:</p>

<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">with</span> (Math){x = cos(<span style="color:red">2</span>)}; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;with (Math){x = cos(2)}; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_width" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<p>由于一些安全原因，在作用域 eval() 创建的变量不能被调用：</p>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">eval</span> (<span style="color:brown">"var x = 2"</span>);<br>
alert (x);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;eval (&quot;var x = 2&quot;);
alert (x);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_eval2" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<p>禁止this关键字指向全局对象。</p>
<pre class="prettyprint prettyprinted" style=""><span class="kwd">function</span><span class="pln"> f</span><span class="pun">(){</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd">return</span><span class="pln"> </span><span class="pun">!</span><span class="kwd">this</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln"> 
</span><span class="com">// 返回false，因为"this"指向全局对象，"!this"就是false</span><span class="pln">

</span><span class="kwd">function</span><span class="pln"> f</span><span class="pun">(){</span><span class="pln"> 
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="str">"use strict"</span><span class="pun">;</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd">return</span><span class="pln"> </span><span class="pun">!</span><span class="kwd">this</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln"> 
</span><span class="com">// 返回true，因为严格模式下，this的值为undefined，所以"!this"为true。</span><span class="pln">
</span><button class="copy-code-button" type="button" data-clipboard-text="function f(){
&nbsp;&nbsp;&nbsp;&nbsp;return !this;
} 
// 返回false，因为&quot;this&quot;指向全局对象，&quot;!this&quot;就是false

function f(){ 
&nbsp;&nbsp;&nbsp;&nbsp;&quot;use strict&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;return !this;
} 
// 返回true，因为严格模式下，this的值为undefined，所以&quot;!this&quot;为true。

"></button></pre>
<p>因此，使用构造函数时，如果忘了加new，this不再指向全局对象，而是报错。</p>
<pre class="prettyprint prettyprinted" style=""><span class="kwd">function</span><span class="pln"> f</span><span class="pun">(){</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="str">"use strict"</span><span class="pun">;</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span><span class="pln">
</span><span class="pun">};</span><span class="pln">
f</span><span class="pun">();</span><span class="com">// 报错，this未定义</span><button class="copy-code-button" type="button" data-clipboard-text="function f(){
&nbsp;&nbsp;&nbsp;&nbsp;&quot;use strict&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;this.a = 1;
};
f();// 报错，this未定义
"></button></pre>
<hr>
<h2>保留关键字</h2>
<p>为了向将来Javascript的新版本过渡，严格模式新增了一些保留关键字：</p>
<ul>
	<li>implements</li>
	<li>interface</li>
	<li>let</li>
    <li>package</li>
	<li>private</li>
	<li>protected</li>
	<li>public</li>
	<li>static</li>
	<li>yield</li>
</ul>
<div class="example">
<div class="example_code"><span style="color:black">
<span style="color:brown">"use strict"</span>;<br><span style="color:mediumblue">var</span> <span style="color:mediumblue">public</span> = <span style="color:red">1500</span>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:green">// 报错
</span></span><button class="copy-code-button" type="button" data-clipboard-text="&quot;use strict&quot;;var public = 1500;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 报错"></button></div>
<br>
	<a target="_blank" href="/try/try.php?filename=tryjs_strict_names" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>
<br>
<table class="lamp"><tbody><tr>
<th style="width:34px"><img decoding="async" src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px"></th>
<td> "use strict" 指令只允许出现在脚本或函数的开头。</td>
</tr></tbody></table>			<!-- 其他扩展 -->
						
			</div>
			
		</div>